<?php

namespace Rbac\Controller;
use Rbac\C\DataConverter;
use Rbac\C\Xpublic;
/**
 *  StudentController
 *  学生信息控制器
 * @author Jonas
 */
class StudentController extends ExtendController {

    /**
     * 显示数据
     */
    public function index() {
        $modelName = $_GET["cmd"];
        $map = array();
        $filterFunction= "_filter".$modelName;
        if(method_exists($this,$filterFunction)){
            $map = $this->$filterFunction();
        }
        $model = D($modelName);
        $data = null;
        if (!empty($model)) {
            $data = $this->_list($model, $map);
        }

        if ($data["rows"] == null) {
            $data["rows"] = array();
        }
        $processFunction = "_process".$modelName;
        if(method_exists($this,$processFunction)){
            $data = $this->$processFunction($data);
        }
        //过滤数据
        $this->ajaxReturn($data);
    }
    
    /**
     * 数据搜索
     * @return type
     */
    protected function _filterInfoStudent(){
        $map = array();
        if(!empty($_POST["is_id"])){
            $map["is_id"] = $_POST["is_id"];
        }
        return $map;
    }

    /**
     * 学生数据再次解析
     * @param type $studentList
     * @return type
     */
    protected function _processInfoStudent($studentList){
        $infoClassModel = M("info_class");
        foreach($studentList["rows"] as $key=>$student){
            //获取班级的信息
            $map["icl_id"] = $student["icl_id"]; 
            $infoClass = $infoClassModel->field("icl_number")->where($map)->find();
            $student["icl_number"] = $infoClass["icl_number"];
            $studentList["rows"][$key] = $student;
        }
        return $studentList;
    }

    /**
    *上传学生信息第一步
    **/
    public function uploadStudentStep1(){
        \Fn\App::setTimeLimit(3600);
         if (empty($_FILES)) {
            $this->ajaxReturn(array("success" => false, "message" => "上传数据为空"));
            exit;
        }
        $importData = new Xpublic\ImportData();
        //读取字段
        $dataFiled = DataConverter\DataConverterForStudent::getField();
        $importData->setMatchFieldParam("name");
        if (!empty($_POST["splitFlag"])) {
            $splitFlag = I("post.splitFlag");
            if ($splitFlag === "t") {
                $splitFlag = "\t";
            }
        }
        //解析数据
        $tempFiledData = $importData->step1($_FILES, $dataFiled, $splitFlag);
        session("upload_erp_pre_orders_filePath", $tempFiledData["tempFilePath"]);
        $this->ajaxReturn(array(
            "success" => true,
            "rows" => $tempFiledData["data"],
            "sqlField" => $dataFiled
        ));
    }

     /**
     * 上传文件第二步
     */
    public function uploadStudentStep2() {
        \Fn\App::setTimeLimit(3600);
        $tempFilePath = session("upload_erp_pre_orders_filePath"); // 得到临时文件地址
        $field = $_POST ["field"]; // 获取提交表单中的字段映射关系
        $importData = new Xpublic\ImportData();
        $data = $importData->step2($tempFilePath, $field);
        $result = DataConverter\DataConverterForStudent::importData($data);
        $response = array(
            "success" => true,
            "message" => "共成功添加" . $result . "条记录"
        );
        $this->ajaxReturn($response);
    }

    /**
     * 学生信息
     */
    public function student() {
        $this->display();
    }
    
    /**
     * 学生个人中心
     */
    public function profile(){
        $this->display();
    }
    
    /**
     * 修改密码
     */
    
    public function passwd(){
        $this->display();
    }
    
    /**
     * 修改学生密码操作
     */
    public function updatepasswd(){
        $passwd = $_POST["new_passwd"];
        $is_id = $_SESSION[C('USER_AUTH_KEY')];
        if(empty($is_id)){
            $result["success"] = false;
            $result["message"] = "会话失效，修改密码失败，请重新登陆";
        }
        $res = M("info_student")->where(array("is_id"=>$is_id))
                                ->save(array("is_password"=>$passwd));
        if($res!==false){
            $result["success"] = true;
        }else{
            $result["success"] = false;
            $result["message"] = "修改密码失败，请重新尝试";
        }
        $this->ajaxReturn($result);
    }

}
